﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.LapPhieu
{
    public partial class BanSachGUI : UserControl, GUI.UctrlGUI
    {
        int _KhachHangID;
        BLL.LapHoaDonBanSachBLL _LapHoaDonBanSachBLL;
        BLL.QuiDinhBLL _QuiDinhBLL;

        public BanSachGUI()
        {
            InitializeComponent();
            _KhachHangID = -1;
            _LapHoaDonBanSachBLL = new BLL.LapHoaDonBanSachBLL();
            _QuiDinhBLL = new BLL.QuiDinhBLL();
        }

        private void btn_TimKhachHang_Click(object sender, EventArgs e)
        {
            Form frm = GUI.qlnsGUI.isOpenForm(typeof(GUI.TraCuu_Update.KhachHangFrm));
            if (frm != null)
            {
                (frm as GUI.TraCuu_Update.KhachHangFrm).SetCallback(SetKhachHangFromTimKhachHang);
                frm.Focus();
            }
            else
            {
                frm = new GUI.TraCuu_Update.KhachHangFrm(SetKhachHangFromTimKhachHang);
                frm.Show(this);
            }
        }

        private void btn_TimSach_Click(object sender, EventArgs e)
        {
            Form frm = GUI.qlnsGUI.isOpenForm(typeof(GUI.TraCuu_Update.SachFrm));
            if (frm != null)
            {
                (frm as GUI.TraCuu_Update.SachFrm).SetCallback(SetSachFromTimSach);
                frm.Focus();
            }
            else
            {
                frm = new GUI.TraCuu_Update.SachFrm(SetSachFromTimSach);
                frm.Show(this);
            }
        }

        public String getName()
        {
            return "LẬP HÓA ĐƠN BÁN SÁCH";
        }

        private void SetKhachHangFromTimKhachHang(DTO.KhachHangDTO dto)
        {
            _KhachHangID = dto.ID;
            txt_HoTen.Text = dto.HoTen;
            txt_DienThoai.Text = dto.DienThoai;
            txt_Email.Text = dto.Email;
            txt_DuNo.Text = dto.DuNo.ToString();
        }

        private void SetSachFromTimSach(DTO.DauSachDTO dto)
        {
            DataGridViewRow curRow = gv_List.CurrentRow;
            if (curRow == null)
                curRow = gv_List.Rows[gv_List.Rows.Count - 1];
            curRow.Cells["ID"].Value = dto.ID.ToString();
            curRow.Cells["Ten"].Value = dto.Ten;
            curRow.Cells["TheLoai"].Value = dto.TheLoai;
            curRow.Cells["SoLuong"].Value = String.Empty;
            //gv_List.Rows[gv_List.Rows.Count - 1].Cells["ID"].Value = dto.ID.ToString();
            //gv_List.Rows[gv_List.Rows.Count - 1].Cells["Ten"].Value = dto.Ten;
            //gv_List.Rows[gv_List.Rows.Count - 1].Cells["TheLoai"].Value = dto.TheLoai;
            //gv_List.Rows[gv_List.Rows.Count - 1].Cells["SoLuong"].Value = String.Empty;
        }

        private void ResetGui()
        {
            _KhachHangID = -1;
            txt_HoTen.Text = String.Empty;
            txt_Email.Text = String.Empty;
            txt_DuNo.Text = String.Empty;
            txt_DienThoai.Text = String.Empty;
        }

        private void BanSachGUI_Load(object sender, EventArgs e)
        {
            ResetGui();

            gv_List.Rows[gv_List.Rows.Count - 1].Cells["STT"].Value =
                gv_List.Rows.Count.ToString();
            gv_List.Rows[gv_List.Rows.Count - 1].Cells["ID"].Value =
                (-1).ToString();

            Form frm = GUI.qlnsGUI.isOpenForm(typeof(GUI.TraCuu_Update.SachFrm));
            if (frm != null)
                (frm as GUI.TraCuu_Update.SachFrm).SetCallback(SetSachFromTimSach);

            frm = GUI.qlnsGUI.isOpenForm(typeof(GUI.TraCuu_Update.KhachHangFrm));
            if (frm != null)
                (frm as GUI.TraCuu_Update.KhachHangFrm).SetCallback(SetKhachHangFromTimKhachHang);
        }

        private void gv_List_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            gv_List.Rows[gv_List.Rows.Count - 1].Cells["STT"].Value =
                gv_List.Rows.Count.ToString();
            gv_List.Rows[gv_List.Rows.Count - 1].Cells["ID"].Value =
                (-1).ToString();
        }

        private void btn_CapNhat_Click(object sender, EventArgs e)
        {
            List<BLL.LapHoaDonBanSachBLL.SachOnGui> listSach =
                new List<BLL.LapHoaDonBanSachBLL.SachOnGui>();
            foreach (DataGridViewRow r in gv_List.Rows)
            {
                if (r.Cells["DonGia"].Value == null &&
                    r.Cells["SoLuong"].Value == null)
                {
                    continue;
                }
                if (
                    r.Cells["DonGia"].Value == null||
                    r.Cells["SoLuong"].Value == null)
                {
                    if (MessageBox.Show(
                        "Giá Trị Không Hợp Lệ, Bỏ Qua Sách Thứ " +
                        r.Cells["STT"].Value.ToString(),
                        "", MessageBoxButtons.YesNo)
                       == DialogResult.Yes)
                    {
                        continue;
                    }
                    else
                        return;
                }

                BLL.LapHoaDonBanSachBLL.SachOnGui dto =
                    new BLL.LapHoaDonBanSachBLL.SachOnGui();
                dto.IDSach = int.Parse(r.Cells["ID"].Value.ToString());
                dto.SoLuong = int.Parse(r.Cells["SoLuong"].Value.ToString());
                dto.DonGia = float.Parse(r.Cells["DonGia"].Value.ToString());
                listSach.Add(dto);
            }
            if (listSach.Count < 1)
            {
                MessageBox.Show("Phải Có Ít Nhất 1 Đầu Sách");
                return;
            }
            if (_KhachHangID < 0)
            {
                MessageBox.Show("Chọn Khách Hàng Trước");
                return;
            }
            float duno = float.Parse(txt_DuNo.Text);
            int rv = _LapHoaDonBanSachBLL.LapPhieu(dt_NgayNhap.Value,_KhachHangID, listSach,
                ref duno);
            if (rv != 0)
                MessageBox.Show("Không Thể Lập Phiếu, Dư Nợ Tối Đa Là " +
                    _QuiDinhBLL.NoToiDa.ToString() +
                    ", Tồn Tối Thiểu Là " + _QuiDinhBLL.TonToiThieu.ToString());
            else
            {
                txt_DuNo.Text = duno.ToString();
                gv_List.Rows.Clear();
                gv_List.Rows[gv_List.Rows.Count - 1].Cells["STT"].Value =
                gv_List.Rows.Count.ToString();
                gv_List.Rows[gv_List.Rows.Count - 1].Cells["ID"].Value =
                    (-1).ToString();
            }
        }

        private void btn_LamLai_Click(object sender, EventArgs e)
        {
            gv_List.Rows.Clear();

            gv_List.Rows[gv_List.Rows.Count - 1].Cells["STT"].Value =
               gv_List.Rows.Count.ToString();
            gv_List.Rows[gv_List.Rows.Count - 1].Cells["ID"].Value =
                (-1).ToString();
        }

        private void gv_List_EditingControlShowing(object sender, 
            DataGridViewEditingControlShowingEventArgs e)
        {
            int clidx = (sender as DataGridView).CurrentCell.ColumnIndex;
            if (clidx == 4) //soluong
            {
                e.Control.KeyPress -= new KeyPressEventHandler(DonGia_KeyPress);
                e.Control.KeyPress -= new KeyPressEventHandler(SoLuong_KeyPress);
                e.Control.KeyPress += new KeyPressEventHandler(SoLuong_KeyPress);
            }
            else if (clidx == 5) // dongia
            {
                e.Control.KeyPress -= new KeyPressEventHandler(SoLuong_KeyPress);
                e.Control.KeyPress -= new KeyPressEventHandler(DonGia_KeyPress);
                e.Control.KeyPress += new KeyPressEventHandler(DonGia_KeyPress);
            }
        }

        private void SoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) &&
                !char.IsDigit(e.KeyChar))
                e.Handled = true;
        }

        private void DonGia_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) &&
                !char.IsDigit(e.KeyChar) &&
                e.KeyChar != '.')
                e.Handled = true;
            if (e.KeyChar == '.' &&
                (sender as TextBox).Text.IndexOf('.') > -1)
                e.Handled = true;
        }
    }
}
